package org.ykhl.mq.util;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ibm on 2016/8/24.
 */
public class HttpUtil {
    /**
     * 请求字段中解析出参数名称和值并放入Map中
     * 请求参数如 ws=getpi&pi=3421240&secret=1239863057
     * @param request
     * @return
     */
    private int timeOut =60000;
    public Map getMapParameters(String request){

        String[] keyvalue = request.split("&");//先根据&进行拆分
        Map<String, Object> parameters = new HashMap<String, Object>();
        for(int i=0;i<keyvalue.length;i++){
            String[] temp =keyvalue[i].split("=");//然后在根据“=”进行拆分
            if(temp.length==2){
                parameters.put(temp[0],temp[1]);
            }
        }
        return parameters;
    }
    public String get(String url)throws IOException {

        String result = get(url,timeOut);
        return result;
    }
    public String get(String url,int timeOut)throws IOException {
        String entity="";
        HttpClient httpClient = new DefaultHttpClient();
        try {
            HttpGet httpget  = new HttpGet(url);
            httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,timeOut);//连接时间
            httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,timeOut);//数据传输时间
            HttpResponse response = httpClient.execute(httpget);
            int   statusCode   = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                entity = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
            httpget.abort();
        }finally {
            httpClient.getConnectionManager().shutdown();
        }
        return entity;
    }

    public  String post(String url,Map<String, Object> parameters)throws  IOException{
        String result =  post(url,parameters,timeOut);
        return result;
    }
    /**
     * Post 方法调用
     * @param url
     * @param parameters
     * @return
     */
    public  String post(String url,Map<String, Object> parameters,int timeOut)throws  IOException{

        HttpClient httpClient = new DefaultHttpClient();
        String content = null;
        try {
            HttpPost httpPost = new HttpPost(url);
            httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,timeOut);//连接时间
            httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,timeOut);//数据传输时间
            HttpEntity entity = createFormEntity(parameters);
            httpPost.setEntity(entity);

            HttpResponse response=httpClient.execute(httpPost);
            content = EntityUtils.toString(response.getEntity(),"UTF-8");
        } finally {
            httpClient.getConnectionManager().shutdown();
        }
        return content;
    }

    private HttpEntity createFormEntity(Map<String, Object> parameters) throws UnsupportedEncodingException {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        for (Map.Entry<String, ? extends Object> parameter : parameters.entrySet()) {
            params.add(new BasicNameValuePair(parameter.getKey(),  toString(parameter.getValue())));
        }

        return new UrlEncodedFormEntity(params, "utf-8");

    }

    private String toString(Object value) {
        return value != null ? value.toString() : null;

    }
}
